package com.doraro.coal.moudle.purch.controller;


import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.doraro.coal.exception.R;
import com.doraro.coal.moudle.purch.entity.PurchExec;
import com.doraro.coal.moudle.purch.entity.PurchSupplier;
import com.doraro.coal.moudle.purch.entity.vo.PurchExecList;
import com.doraro.coal.moudle.purch.enums.ExecStatus;
import com.doraro.coal.moudle.purch.service.IPurchExecService;
import com.doraro.coal.moudle.purch.service.IPurchSupplierService;
import com.doraro.coal.utils.ShiroUtils;
import com.doraro.coal.vo.PageParam;
import com.doraro.coal.vo.PageResult;
import com.doraro.coal.vo.UserDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author doraro
 * @since 2019-10-10
 */
@RestController
@RequestMapping("/purchExec")
public class PurchExecRestController {
    @Autowired
    private IPurchExecService execService;
    @Autowired
    private IPurchSupplierService supplierService;
    @PostMapping("/PEList")
    public R<PageResult<PurchExecList>> peList(@RequestBody PageParam<PurchExec> searchMap){
        final Page<PurchExec> purchExecPage = searchMap.toPage();
        final UserDto userEntity = ShiroUtils.getUserEntity();
        IPage<PurchExecList> page = execService.listPage(purchExecPage,searchMap);
        return R.success(new PageResult<>(page.getTotal(),page.getRecords()) );
    }

    @PostMapping("/updatePE.do")
    @Transactional(rollbackFor = Exception.class)
    public R updatePE(@RequestBody List<PurchSupplier> suppliers){
        if (CollectionUtil.isEmpty(suppliers)) {
            return R.fail("参数不能为空");
        }
        final PurchSupplier purchSupplier = suppliers.get(0);
        execService.lambdaUpdate().eq(PurchExec::getId, purchSupplier.getExecId())
                .set(PurchExec::getExecStatus, ExecStatus.Submit)
                .update();
        supplierService.updateBatchById(suppliers);
        return R.success();
    }
}

